
from . import settings  # 包内部，使用相对导入
from qcloudsms_py import SmsSingleSender

# 生成验证码函数（4位数字）
import random
from utils.logging import get_logger
logger=get_logger('sms')
def get_code():
    code = ''
    for i in range(4):
        code += str(random.randint(0, 9))
    return code


# 发送验证码的函数


def send_code(mobile_phone:str, code:str, min=3):
    # 放到配置文件中
    appid = settings.APPID
    appkey = settings.APPKEY
    template_id = settings.TEMPLATE_ID
    sms_sign = settings.SMS_SING
    phone_numbers = [mobile_phone, ]
    ssender = SmsSingleSender(appid, appkey)
    params = [code, min]
    try:
        result = ssender.send_with_param(86, phone_numbers[0], template_id, params, sign=sms_sign, extend="", ext="")
        if result and result.get('result') == 0:
            return True,'发送成功'
        else:
            logger.error('短信发送失败，失败原因为：%s' % str(result.get('errmsg')))
            return False,str(result.get('errmsg'))
    except Exception as e:
        logger.error('短信发送失败，失败原因为：%s'%str(e))
        return False,str(e)
